package com.justgame.project.service.system.service.basic;

import com.justgame.project.service.system.entity.SysUserInfo;
import com.justgame.project.service.system.query.SysUserInfoQuery;
import com.justgame.project.service.system.wrapper.SysUserInfoWrapper;
import com.justgame.project.base.servcice.IBasicService;
import java.util.Optional;
import org.springframework.util.Assert;
import java.time.LocalDateTime;
import org.springframework.lang.NonNull;
import java.lang.String;
import com.justgame.project.cloud.base.entity.StringEncrypt;

/**
 *
 * 用户信息表服务自定义查询类
 *
 * @@tableName sys_user_info
 * @author Zwb
 */
@SuppressWarnings("all")
public interface BasicSysUserInfoService extends IBasicService<SysUserInfoQuery,SysUserInfo,SysUserInfoWrapper> {

   /**
    * 注册账号
    * @param userName 账号
    * @param password 密码
    * @param phoneNumber 手机号
    * @param openId 微信授权id
    * @param status 状态
    * @param sex 性别
    * @param role 角色
    * @param lastAccessDate 最后访问日期
    */
   default SysUserInfo wechatUserRegister(String userName, StringEncrypt password, String phoneNumber, String openId){
      Assert.notNull(userName,"必须提供账号");
      Assert.notNull(openId,"必须提供微信授权id");
      Assert.notNull(password,"必须提供密码");
      Assert.isTrue(phoneNumber != null && phoneNumber.matches("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"),"手机号格式不正确");
      SysUserInfo sysUserInfo = new SysUserInfo();
      sysUserInfo.setUserName(safeValue(userName, null));
      sysUserInfo.setPassword(safeValue(password, null));
      sysUserInfo.setPhoneNumber(safeValue(phoneNumber, null));
      sysUserInfo.setOpenId(safeValue(openId, null));
      sysUserInfo.setStatus(SysUserInfo.Status.HEALTH);
      sysUserInfo.setSex(SysUserInfo.Sex.UNKNOWN);
      sysUserInfo.setRole(SysUserInfo.Role.USER);
      sysUserInfo.setLastAccessDate(LocalDateTime.now());
      save(sysUserInfo);
      return sysUserInfo;
   }

   /**
    * 通过邮箱获取用户信息
    * @param emailIs 电子邮箱
    */
   default Optional<SysUserInfo> findByEmailIs(String emailIs){
      Assert.isTrue(emailIs != null && emailIs.matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"),"电子邮箱格式不正确");
      return Optional.ofNullable(getOne(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getEmail,emailIs)));
   }

   /**
    * 通过邮箱获取用户信息
    * @param phoneNumberIs 手机号
    */
   default Optional<SysUserInfo> findByPhoneNumberIs(String phoneNumberIs){
      Assert.isTrue(phoneNumberIs != null && phoneNumberIs.matches("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"),"手机号格式不正确");
      return Optional.ofNullable(getOne(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getPhoneNumber,phoneNumberIs)));
   }

   /**
    * 更新邮箱
    * @param email 电子邮箱
    */
   default boolean updateByEmail(@NonNull SysUserInfo sysUserInfo,String email){
      Assert.isTrue(email != null && email.matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"),"电子邮箱格式不正确");
      Assert.notNull(sysUserInfo.getId(),"必须提供主键ID");
      sysUserInfo.setEmail(safeValue(email, null));
      return updateById(sysUserInfo);
   }

   /**
    * 更新相关字段：微信授权id
    * @param openId 微信授权id
    */
   default boolean updateByOpenId(@NonNull SysUserInfo sysUserInfo,String openId){
      Assert.notNull(openId,"必须提供微信授权id");
      Assert.notNull(sysUserInfo.getId(),"必须提供主键ID");
      sysUserInfo.setOpenId(safeValue(openId, null));
      return updateById(sysUserInfo);
   }

   /**
    * 查询通过字段：账号、密码
    * @param userNameIs 账号
    * @param passwordIs 密码
    */
   default Optional<SysUserInfo> findByUserNameIsAndPasswordIs(String userNameIs, StringEncrypt passwordIs){
      Assert.notNull(userNameIs,"必须提供账号");
      Assert.notNull(passwordIs,"必须提供密码");
      return Optional.ofNullable(getOne(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getUserName,userNameIs)
                  .eq(SysUserInfo::getPassword,passwordIs)));
   }

   /**
    * 查询通过字段：微信授权id
    * @param openIdIs 微信授权id
    */
   default Optional<SysUserInfo> findByOpenIdIs(String openIdIs){
      Assert.notNull(openIdIs,"必须提供微信授权id");
      return Optional.ofNullable(getOne(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getOpenId,openIdIs)));
   }

   /**
    * 查询通过字段：账号
    * @param userNameIs 账号
    */
   default long countByUserNameIs(String userNameIs){
      Assert.notNull(userNameIs,"必须提供账号");
      return count(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getUserName,userNameIs));
   }

   /**
    * 查询通过字段：电子邮箱
    * @param emailIs 电子邮箱
    */
   default long countByEmailIs(String emailIs){
      Assert.notNull(emailIs,"必须提供电子邮箱");
      return count(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getEmail,emailIs));
   }

   /**
    * 查询通过字段：手机号
    * @param phoneNumberIs 手机号
    */
   default long countByPhoneNumberIs(String phoneNumberIs){
      Assert.notNull(phoneNumberIs,"必须提供手机号");
      return count(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getPhoneNumber,phoneNumberIs));
   }

   /**
    * 查询通过字段：微信授权id
    * @param openIdIs 微信授权id
    */
   default long countByOpenIdIs(String openIdIs){
      Assert.notNull(openIdIs,"必须提供微信授权id");
      return count(SysUserInfoQuery.builder().getWrapper()
                  .eq(SysUserInfo::getOpenId,openIdIs));
   }

   /**
    * 新增创建
    * @param userName 账号
    * @param password 密码
    * @param sex 性别
    * @param status 状态
    * @param email 电子邮箱
    * @param phoneNumber 手机号
    * @param openId 微信授权id
    * @param role 角色
    * @param lastAccessDate 最后访问日期
    */
   default SysUserInfo baseAdd(String userName, StringEncrypt password, SysUserInfo.Sex sex, SysUserInfo.Status status, String email, String phoneNumber, String openId, SysUserInfo.Role role, LocalDateTime lastAccessDate){
      SysUserInfo sysUserInfo = new SysUserInfo();
      sysUserInfo.setUserName(safeValue(userName, null));
      sysUserInfo.setPassword(safeValue(password, null));
      sysUserInfo.setSex(safeValue(sex, null));
      sysUserInfo.setStatus(safeValue(status, null));
      sysUserInfo.setEmail(safeValue(email, null));
      sysUserInfo.setPhoneNumber(safeValue(phoneNumber, null));
      sysUserInfo.setOpenId(safeValue(openId, null));
      sysUserInfo.setRole(safeValue(role, null));
      sysUserInfo.setLastAccessDate(safeValue(lastAccessDate, null));
      save(sysUserInfo);
      return sysUserInfo;
   }

   /**
    * 更新
    * @param userName 账号
    * @param password 密码
    * @param sex 性别
    * @param status 状态
    * @param email 电子邮箱
    * @param phoneNumber 手机号
    * @param openId 微信授权id
    * @param role 角色
    * @param lastAccessDate 最后访问日期
    */
   default boolean baseUpdate(@NonNull SysUserInfo sysUserInfo,String userName, StringEncrypt password, SysUserInfo.Sex sex, SysUserInfo.Status status, String email, String phoneNumber, String openId, SysUserInfo.Role role, LocalDateTime lastAccessDate){
      Assert.notNull(userName,"必须提供账号");
      Assert.notNull(email,"必须提供电子邮箱");
      Assert.notNull(openId,"必须提供微信授权id");
      Assert.notNull(password,"必须提供密码");
      Assert.notNull(role,"必须提供角色");
      Assert.notNull(status,"必须提供状态");
      Assert.notNull(sex,"必须提供性别");
      Assert.notNull(lastAccessDate,"必须提供最后访问日期");
      Assert.notNull(phoneNumber,"必须提供手机号");
      Assert.notNull(sysUserInfo.getId(),"必须提供主键ID");
      sysUserInfo.setUserName(safeValue(userName, null));
      sysUserInfo.setPassword(safeValue(password, null));
      sysUserInfo.setSex(safeValue(sex, null));
      sysUserInfo.setStatus(safeValue(status, null));
      sysUserInfo.setEmail(safeValue(email, null));
      sysUserInfo.setPhoneNumber(safeValue(phoneNumber, null));
      sysUserInfo.setOpenId(safeValue(openId, null));
      sysUserInfo.setRole(safeValue(role, null));
      sysUserInfo.setLastAccessDate(safeValue(lastAccessDate, null));
      return updateById(sysUserInfo);
   }

}